home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 August / CICA - The Ultimate Collection of Shareware for Windows (Disc 2) (August 1995).iso / disc2 / demo / pwrtcp11.exe / DGC.C_ / DGC.bin
Text File  |  1995-02-17  |  21KB  |  712 lines

  1. #pragma warning(disable : 4001) /* Allow // comments */
  2. //***************************************************************************
  3. //
  4. //  Module: dgc.c
  5. //
  6. //    Tab setting: 4
  7. //
  8. //***************************************************************************
  9. //
  10. //  Written by Dart Communication Application Programming Group.
  11. //  Copyright (c) 1994 Dart Communications.  All Rights Reserved.
  12. //
  13. //  Purpose: Provides access to functions in UDP interface
  14. //             DLL via dialog box.
  15. //
  16. //    Author:  Roger Lathrop
  17. //
  18. //    History: 12/94 - First version.
  19. //
  20. //***************************************************************************
  21.  
  22. #include <stdlib.h>
  23. #include <malloc.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26.  
  27. // UDP Include files. Common brings in windows.h for us.
  28. #include "..\..\include\common.h"
  29. #include "..\..\include\powertcp.h"
  30.  
  31. // Our resource file header.
  32. #include "dgc.hh"
  33.  
  34.  
  35.  
  36. /****************************************************************************
  37. * Static variables for module.
  38. ****************************************************************************/
  39. static HICON     hOurIcon;        // Handle to our application icon.
  40.  
  41. // Proc instance address for callback functions.
  42. static CONNECTUDPEVENT      lpfnConnectUdpEvent;
  43. static EXCEPTIONUDPEVENT lpfnExceptionUdpEvent;
  44. static RECVUDPEVENT      lpfnRecvUdpEvent;
  45. static SENDEVENT          lpfnSendEvent;
  46.  
  47.  
  48. #define PORT_1    0        // Port controlled by left side of dialog box
  49. #define PORT_2    1       // Port controlled by right side of dialog box
  50.  
  51. // Macro to get control id from Port and left hand side control id.
  52. // Control id's on right are left id + 100.
  53. #define PORTCTLID(Port,ctlid) ((WORD)((Port==PORT_1) ? (ctlid) : (ctlid+100)))
  54.  
  55. static HWND hOurWnd;            // Our window handle
  56.  
  57. static HPOWERTCP hMyPowerTcp[2]; // Handles to PowerTCP session.
  58.  
  59.  
  60.  
  61. /****************************************************************************
  62. * Routines for fetching parameters from edit controls.
  63. ****************************************************************************/
  64. //---------------------------------------------------------------------------
  65. // Name.......: GetStringParam
  66. // Purpose....: Get a string from an edit control.
  67. //
  68. // Parameters.: CtlId    - WORD Control id of edit control
  69. //              Required - BOOL Parameter is required.
  70. //
  71. // Returns....: Malloced pointer to string, or NULL if malloc fails
  72. //---------------------------------------------------------------------------
  73. static char * GetStringParam(WORD CtlId,BOOL Required)
  74.     { char *p;
  75.       WORD len;
  76.  
  77.     // Get length of string in edit control. If length is 0,
  78.     // and the parameter is required, then ding the speaker,
  79.     // set focus to control, and put message in status bar.
  80.     len = (WORD)SendDlgItemMessage(hOurWnd,CtlId,WM_GETTEXTLENGTH,0,0);
  81.     if(len == 0 && Required)
  82.         {
  83.         MessageBeep(0);
  84.         SetFocus(GetDlgItem(hOurWnd,CtlId));
  85.         return NULL;
  86.         }
  87.  
  88.     // Allocate space for string and a NULL byte.
  89.     len++;
  90.     p = malloc(len);
  91.  
  92.     // Malloc should never fail, unless we have a memory leak,
  93.     // but best to check anyway.
  94.     if(p)
  95.         { // Extract string from edit control.
  96.         SendDlgItemMessage(hOurWnd,CtlId,WM_GETTEXT,len,(LPARAM)(LPSTR)p);
  97.         }
  98.  
  99.     return p;
  100.     }
  101.  
  102. //---------------------------------------------------------------------------
  103. // Name.......: FreeStringParam
  104. // Purpose....: Free a string allocated by GetStringParam.
  105. //                Checks for NULL string so caller doesn't have to.
  106. //
  107. // Parameters.: s    - Char * String from GetStringParam
  108. //
  109. // Returns....: void
  110. //---------------------------------------------------------------------------
  111. static void FreeStringParam(char *s)
  112.     {
  113.     if(s)
  114.         {
  115.         free(s);
  116.         }
  117.     }
  118.  
  119. //---------------------------------------------------------------------------
  120. // Name.......: GetWordParam
  121. // Purpose....: Get an int from an edit control.
  122. //
  123. // Parameters.: CtlId    - WORD Control id of edit control
  124. //                pval    - LPWORD far pointer to area to receive value.
  125. //
  126. // Returns....: True if numeric value in edit control.
  127. //---------------------------------------------------------------------------
  128. BOOL GetWordParam(WORD CtlId,LPWORD pval)
  129.     { char *p;
  130.       char *psave;
  131.  
  132.     // Get text from edit control. Skip leading whitespace, then
  133.     // if first character is a digit, convert string to long.
  134.     // If not a digit, ding the speaker and set focus to control.
  135.     p = GetStringParam(CtlId,TRUE);
  136.     psave = p;
  137.     if(p)
  138.         {
  139.         while(isspace(*p))
  140.             {
  141.             p++;
  142.             }
  143.  
  144.         if(isdigit(*p))
  145.             {
  146.             *pval = (WORD)atoi(p);
  147.             free(psave);
  148.             return TRUE;
  149.             }
  150.         else
  151.             {
  152.             MessageBeep(0);
  153.             SetFocus(GetDlgItem(hOurWnd,CtlId));
  154.             free(psave);
  155.             *pval = 0;
  156.             return FALSE;
  157.             }
  158.         }
  159.  
  160.     return FALSE;
  161.     }
  162.  
  163. //---------------------------------------------------------------------------
  164. // Name.......:ReadIniVal
  165. // Purpose....:Reads one ini value
  166. //---------------------------------------------------------------------------
  167. static void ReadIniVal(char *name,WORD id)
  168.     { static char Value[128];
  169.  
  170.     GetPrivateProfileString("DGC",
  171.                             name,
  172.                             "",
  173.                             Value,
  174.                             sizeof Value,
  175.                             "DGC.INI");
  176.     SendDlgItemMessage(hOurWnd,id,WM_SETTEXT,0,(LPARAM)(LPCSTR)Value);
  177.  
  178.     }
  179.  
  180. //---------------------------------------------------------------------------
  181. // Name.......:WriteIniVal
  182. // Purpose....:Writes one ini value
  183. //---------------------------------------------------------------------------
  184. static void WriteIniVal(char *name,WORD id)
  185.     { char *Value;
  186.  
  187.     Value = GetStringParam(id,FALSE);
  188.     if(Value)
  189.         {
  190.         WritePrivateProfileString("DGC",
  191.                                   name,
  192.                                   Value,
  193.                                   "DGC.INI");
  194.         FreeStringParam(Value);
  195.         }
  196.  
  197.     }
  198.  
  199. //---------------------------------------------------------------------------
  200. // Name.......:ReadIniFile
  201. // Purpose....:Reads default values from INI file.
  202. //---------------------------------------------------------------------------
  203. static void ReadIniFile(void)
  204.     {
  205.     ReadIniVal("OEMLICENSE",EDC_LICENSE);
  206.     ReadIniVal("DATAG_1",EDC_SND_DATAG_1);
  207.     ReadIniVal("DATAG_2",EDC_SND_DATAG_2);
  208.     ReadIniVal("ADDR_1",EDC_SND_ADDR_1);
  209.     ReadIniVal("ADDR_2",EDC_SND_ADDR_2);
  210.     ReadIniVal("REPS_1",EDC_SND_REPS_1);
  211.     ReadIniVal("REPS_2",EDC_SND_REPS_2);
  212.     ReadIniVal("SND_PORT_1",EDC_SND_PORT_1);
  213.     ReadIniVal("SND_PORT_2",EDC_SND_PORT_2);
  214.     ReadIniVal("PORT_1",EDC_PORT_1);
  215.     ReadIniVal("PORT_2",EDC_PORT_2);
  216.     ReadIniVal("SND_PORT_1",EDC_SND_PORT_1);
  217.     }
  218.  
  219. //---------------------------------------------------------------------------
  220. // Name.......:WriteIniFile
  221. // Purpose....:Writes current values to ini file
  222. //---------------------------------------------------------------------------
  223. static void WriteIniFile(void)
  224.     {
  225.     WriteIniVal("OEMLICENSE",EDC_LICENSE);
  226.     WriteIniVal("DATAG_1",EDC_SND_DATAG_1);
  227.     WriteIniVal("DATAG_2",EDC_SND_DATAG_2);
  228.     WriteIniVal("ADDR_1",EDC_SND_ADDR_1);
  229.     WriteIniVal("ADDR_2",EDC_SND_ADDR_2);
  230.     WriteIniVal("REPS_1",EDC_SND_REPS_1);
  231.     WriteIniVal("REPS_2",EDC_SND_REPS_2);
  232.     WriteIniVal("SND_PORT_1",EDC_SND_PORT_1);
  233.     WriteIniVal("SND_PORT_2",EDC_SND_PORT_2);
  234.     WriteIniVal("PORT_1",EDC_PORT_1);
  235.     WriteIniVal("PORT_2",EDC_PORT_2);
  236.     WriteIniVal("SND_PORT_1",EDC_SND_PORT_1);
  237.     }
  238. //---------------------------------------------------------------------------
  239. // Name.......: ShowState
  240. // Purpose....: Show the state of a port
  241. //
  242. // Parameters.: Port    -     WORD PORT_1 or PORT_2
  243. //
  244. // Returns....: void
  245. //---------------------------------------------------------------------------
  246. static void ShowState(WORD Port)
  247.     { PT_STATE state;
  248.       char *str;
  249.  
  250.     if(hMyPowerTcp[Port])
  251.         {
  252.         state = StateUdp(hMyPowerTcp[Port]);
  253.         switch(state)
  254.             {
  255.             case PT_INVALID:     str = "Invalid State"; break;
  256.             case PT_CLOSED:     str = "Closed"; break;
  257.             case PT_CONNECTING: str = "Connecting"; break;
  258.             case PT_CONNECTED:  str = "Connected"; break;
  259.             case PT_LISTENING:  str = "Listening"; break;
  260.             case PT_CLOSING:    str = "Closing"; break;
  261.             default:            str = "UNKNOWN state!!!"; break;
  262.             }
  263.         }
  264.     else
  265.         {
  266.         str = "Closed";
  267.         }
  268.  
  269.     SetDlgItemText(hOurWnd,PORTCTLID(Port,STC_STATUS_1),str);
  270.     }
  271.  
  272.  
  273.  
  274. /****************************************************************************
  275. * CALLBACK FUNCTIONS. Called asynchronously by DLL as events occur.
  276. ****************************************************************************/
  277.  
  278. //---------------------------------------------------------------------------
  279. // Name.......: ConnectUdpEvent
  280. // Purpose....: Callback function for connect event notification
  281. //
  282. // Parameters.: hSession        - HPOWERTCP Session id.
  283. //                UserData        - DWORD     User data supplied to loginhost.
  284. //                LocalDotAddr    - LPCSTR Local dot address
  285. //                LocalPort        - WORD   Local port id.
  286. //                LocalName        - LPCSTR Local name.
  287. //                MaxByteCnt        - WORD     Largest datagram.
  288. //
  289. // Returns....: void
  290. //---------------------------------------------------------------------------
  291. void CALLBACK ConnectUdpEvent(HPOWERTCP hSession,
  292.                                               DWORD    UserData,
  293.                                               LPCSTR     LocalDotAddr,
  294.                                               WORD       LocalPort,
  295.                                               LPCSTR     LocalName,
  296.                                          WORD        MaxByteCnt)
  297.     {
  298.     if(hMyPowerTcp[LOWORD(UserData)])
  299.         {
  300.         ShowState((WORD)UserData);
  301.         }
  302.     }
  303.  
  304. //---------------------------------------------------------------------------
  305. // Name.......: ExceptionEvent
  306. // Purpose....: Callback function for exception event notification
  307. //
  308. // Parameters.: hSession    - HPOWERTCP Session id.
  309. //                UserData    - DWORD         PORT_1 or PORT_2
  310. //                 Error        - PT_EXCEPTION Enumerated exception type.
  311. //                ErrorType    - LPCSTR Error type description string.
  312. //                DataTag        - DWORD     If not 0, identifies exception packet.
  313. //
  314. // Returns....: void
  315. //---------------------------------------------------------------------------
  316. void CALLBACK ExceptionUdpEvent(HPOWERTCP hSession,
  317.                                            DWORD UserData,
  318.                                         PT_EXCEPTION Error,
  319.                                         LPCSTR ErrorDesc,
  320.                                         DWORD DataTag)
  321.     {
  322.     SetDlgItemText(hOurWnd,PORTCTLID(UserData,STC_STATUS_1),ErrorDesc);
  323.     }
  324.  
  325. //---------------------------------------------------------------------------
  326. // Name.......: RecvUdpEvent
  327. // Purpose....: Callback function for received data notification
  328. //
  329. // Parameters.: hSession      - HPOWERTCP Session id.
  330. //                UserData      - DWORD     User data supplied to ConnectUdp.
  331. //                Data          - LPVOID    Data from remote source
  332. //                ByteCnt       - size_t    byte count for received data
  333. //                RemoteDotAddr - LPCSTR    source address of datagram
  334. //                RemotePort    - WORD      source port of datagram
  335. //
  336. // Returns....: void
  337. //---------------------------------------------------------------------------
  338. void CALLBACK RecvUdpEvent(HPOWERTCP hSession,
  339.                                    DWORD     UserData,
  340.                                    LPVOID    Data,
  341.                                    size_t    ByteCnt,
  342.                                    LPCSTR    RemoteDotAddr,
  343.                                    WORD      RemotePort)
  344.  
  345.     { WORD sent;
  346.       char *p;
  347.  
  348.     if((Data==NULL) && (ByteCnt == 0))
  349.         { // Special case, indicates port has closed.
  350.         ShowState((WORD)UserData);
  351.         return;
  352.         }
  353.  
  354.     GetWordParam(PORTCTLID(UserData,EDC_RCV_TOTAL_1),&sent);
  355.     sent += (WORD)ByteCnt;
  356.  
  357.     SetDlgItemInt(hOurWnd,PORTCTLID(UserData,EDC_RCV_TOTAL_1),sent,FALSE);
  358.     SetDlgItemInt(hOurWnd,PORTCTLID(UserData,EDC_RCV_PORT_1),RemotePort,FALSE);
  359.     SetDlgItemText(hOurWnd,PORTCTLID(UserData,EDC_RCV_ADDR_1),RemoteDotAddr);
  360.  
  361.     // Duplicate the data so we can NULL terminate it.
  362.     p = malloc(ByteCnt + 1);
  363.     if(p)
  364.         {
  365.         _fmemcpy(p,Data,ByteCnt);
  366.         p[ByteCnt] = '\0';
  367.         SetDlgItemText(hOurWnd,PORTCTLID(UserData,EDC_RCV_DATAG_1),p);
  368.         free(p);
  369.         }
  370.  
  371.     }
  372.  
  373.  
  374. //---------------------------------------------------------------------------
  375. // Name.......: SendEvent
  376. // Purpose....: Callback function for send event notification
  377. //
  378. // Parameters.: hSession    - HPOWERTCP Session id.
  379. //                UserData    - DWORD     PORT_1 or PORT_2
  380. //                 Tag            - DWORD     Size of data packed, see ActionSend
  381. //
  382. // Returns....: void
  383. //---------------------------------------------------------------------------
  384. void CALLBACK SendEvent(HPOWERTCP hSession,DWORD UserData,DWORD Tag)
  385.     { WORD sent;
  386.  
  387.     // Get edit control value, add Tag to it, reset edit control value.
  388.     GetWordParam(PORTCTLID(UserData,EDC_SND_TOTAL_B_1),&sent);
  389.     sent += (WORD)Tag;
  390.     SetDlgItemInt(hOurWnd,PORTCTLID(UserData,EDC_SND_TOTAL_B_1),sent,FALSE);
  391.     }
  392.  
  393.  
  394.  
  395.  
  396. /****************************************************************************
  397. * ActionXxx Functions. Handle user actions, dispatched from MainWndProc.
  398. ****************************************************************************/
  399.  
  400. //---------------------------------------------------------------------------
  401. // Name.......: ActionSend
  402. // Purpose....: Send data
  403. //---------------------------------------------------------------------------
  404. static void ActionSend(WORD Port)
  405.     { char *Data;
  406.       char *Host;
  407.       WORD RemotePort;
  408.       WORD Reps;
  409.       WORD len;
  410.       WORD sent;
  411.  
  412.  
  413.     // Get the string to send, host address, port number, and
  414.     // repeat count.
  415.     Data = GetStringParam(PORTCTLID(Port,EDC_SND_DATAG_1),TRUE);
  416.     if(!Data)
  417.         {
  418.         return;
  419.         }
  420.  
  421.     Host = GetStringParam(PORTCTLID(Port,EDC_SND_ADDR_1),FALSE);
  422.     if(!Host)
  423.         {
  424.         FreeStringParam(Data);
  425.         return;
  426.         }
  427.  
  428.     GetWordParam(PORTCTLID(Port,EDC_SND_PORT_1),&RemotePort);
  429.     GetWordParam(PORTCTLID(Port,EDC_SND_REPS_1),&Reps);
  430.  
  431.  
  432.     len = (WORD)strlen(Data);
  433.  
  434.     // Get current total from control
  435.     GetWordParam(PORTCTLID(Port,EDC_SND_TOTAL_A_1),&sent);
  436.  
  437.     while(Reps--)
  438.         { // Send packet Reps times. len is used as DataTag
  439.           // so that RecvUdpEvent can update totals.
  440.         SendUdp(hMyPowerTcp[Port],
  441.                   Host,
  442.                 RemotePort,
  443.                 Data,
  444.                 len,
  445.                 len);
  446.  
  447.         // Update total sent.
  448.         sent += len;
  449.  
  450.         SetDlgItemInt(hOurWnd,PORTCTLID(Port,EDC_SND_TOTAL_A_1),
  451.                           sent,FALSE);
  452.         }
  453.  
  454.  
  455.     FreeStringParam(Data);
  456.     FreeStringParam(Host);
  457.     }
  458.  
  459. //---------------------------------------------------------------------------
  460. // Name.......: ActionOpen
  461. // Purpose....: Handle BTN_OPEN_X command.
  462. //---------------------------------------------------------------------------
  463. static void ActionOpen(WORD Port)
  464.     { WORD pt;
  465.       BOOL res;
  466.       char *license;
  467.  
  468.     res = GetWordParam(PORTCTLID(Port,EDC_PORT_1),&pt);
  469.     if(!res)
  470.         {
  471.         return;
  472.         }
  473.  
  474.     license = GetStringParam(EDC_LICENSE,FALSE);
  475.  
  476.     hMyPowerTcp[Port] = ConnectUdp( Port,        // User data identifies port.
  477.                                     license,
  478.                                     PT_SHOW,   // show PowerTCP icon
  479.                                     NULL,
  480.                                     pt,
  481.                                     lpfnConnectUdpEvent,
  482.                                     lpfnRecvUdpEvent,
  483.                                     lpfnSendEvent,
  484.                                     lpfnExceptionUdpEvent);
  485.     ShowState(Port);
  486.  
  487.     if(!hMyPowerTcp[Port])
  488.         {
  489.         MessageBox(hOurWnd,"ConnectUdp Failed","UDP Error",MB_OK);
  490.         }
  491.     }
  492.  
  493. //---------------------------------------------------------------------------
  494. // Name.......: ActionClose
  495. // Purpose....: Handle BTN_CLOSE_X command.
  496. //---------------------------------------------------------------------------
  497. static void ActionClose(WORD Port)
  498.     {
  499.     if(hMyPowerTcp[Port])
  500.         {
  501.         CloseUdp(hMyPowerTcp[Port],FALSE);
  502.         hMyPowerTcp[Port] = NULL;
  503.         ShowState((WORD)Port);
  504.         }
  505.     }
  506.  
  507. //---------------------------------------------------------------------------
  508. // Name.......: ActionClear
  509. // Purpose....: Handle BTN_CLEAR command
  510. //---------------------------------------------------------------------------
  511. static void ActionClear(void)
  512.     {
  513.     SetDlgItemInt(hOurWnd,EDC_SND_TOTAL_A_1,0,FALSE);
  514.     SetDlgItemInt(hOurWnd,EDC_SND_TOTAL_B_1,0,FALSE);
  515.     SetDlgItemInt(hOurWnd,EDC_SND_TOTAL_A_2,0,FALSE);
  516.     SetDlgItemInt(hOurWnd,EDC_SND_TOTAL_B_2,0,FALSE);
  517.     SetDlgItemInt(hOurWnd,EDC_RCV_TOTAL_1,0,FALSE);
  518.     SetDlgItemInt(hOurWnd,EDC_RCV_TOTAL_2,0,FALSE);
  519.     SetDlgItemText(hOurWnd,EDC_RCV_DATAG_2,"");
  520.     SetDlgItemText(hOurWnd,EDC_RCV_ADDR_2,"");
  521.     SetDlgItemText(hOurWnd,EDC_RCV_PORT_2,"");
  522.     SetDlgItemText(hOurWnd,EDC_RCV_DATAG_1,"");
  523.     SetDlgItemText(hOurWnd,EDC_RCV_ADDR_1,"");
  524.     SetDlgItemText(hOurWnd,EDC_RCV_PORT_1,"");
  525.     }
  526.  
  527. //---------------------------------------------------------------------------
  528. // Name.......: ActionIDOK
  529. // Purpose....: Handle IDOK (User hits enter)
  530. //---------------------------------------------------------------------------
  531. static void ActionIDOK(void)
  532.     { int id;
  533.     id = GetDlgCtrlID(GetFocus());
  534.     switch(id)
  535.         {
  536.         case EDC_SND_ADDR_1:
  537.         case EDC_SND_PORT_1:
  538.         case EDC_SND_REPS_1:
  539.         case EDC_SND_TOTAL_A_1:
  540.         case EDC_SND_TOTAL_B_1:
  541.         case EDC_SND_DATAG_1: ActionSend(PORT_1); break;
  542.  
  543.         case EDC_SND_ADDR_2:
  544.         case EDC_SND_PORT_2:
  545.         case EDC_SND_REPS_2:
  546.         case EDC_SND_TOTAL_A_2:
  547.         case EDC_SND_TOTAL_B_2:
  548.         case EDC_SND_DATAG_2: ActionSend(PORT_2); break;
  549.  
  550.         default: MessageBeep(0);
  551.         }
  552.     }
  553.  
  554. //---------------------------------------------------------------------------
  555. // Name.......:MainWndProc
  556. // Purpose....:Main windows procedure for UDP Test Program.
  557. //---------------------------------------------------------------------------
  558. static DLGPROC lpfnMainWndProc;
  559.  
  560. BOOL CALLBACK MainWndProc(HWND hWnd, UINT wMessage, WPARAM wParam, LPARAM lParam)
  561.     {
  562.     HDC hDc;
  563.     PAINTSTRUCT ps;
  564.  
  565.     switch(wMessage)
  566.            {
  567.  
  568.            case WM_INITDIALOG :
  569.             hOurWnd = hWnd;
  570.             ReadIniFile();
  571.             ActionClear(); // initialize
  572.             return TRUE;
  573.  
  574.            case WM_COMMAND :
  575.          // Dispatch a button click.
  576.          switch(wParam)
  577.             {
  578.             case BTN_EXIT:
  579.                     if(hMyPowerTcp[PORT_1])
  580.                         {
  581.                         CloseUdp(hMyPowerTcp[PORT_1],TRUE);
  582.                         }
  583.                     if(hMyPowerTcp[PORT_2])
  584.                         {
  585.                         CloseUdp(hMyPowerTcp[PORT_2],TRUE);
  586.                         }
  587.                     WriteIniFile();
  588.                     PostQuitMessage(0);
  589.                     break;
  590.             case IDOK:            ActionIDOK(); break;
  591.             case BTN_OPEN_1:    ActionOpen(PORT_1); break;
  592.             case BTN_OPEN_2:    ActionOpen(PORT_2); break;
  593.             case BTN_CLOSE_1:    ActionClose(PORT_1); break;
  594.             case BTN_CLOSE_2:    ActionClose(PORT_2); break;
  595.             case BTN_CLEAR:        ActionClear(); break;
  596.  
  597.             }
  598.  
  599.           break;
  600.  
  601.        case WM_SYSCOMMAND :
  602.           switch(wParam & 0xFFF0)
  603.           {
  604.               case SC_CLOSE :
  605.                     if(hMyPowerTcp[PORT_1])
  606.                         {
  607.                         CloseUdp(hMyPowerTcp[PORT_1],TRUE);
  608.                         }
  609.                     if(hMyPowerTcp[PORT_2])
  610.                         {
  611.                         CloseUdp(hMyPowerTcp[PORT_2],TRUE);
  612.                         }
  613.                     WriteIniFile();
  614.                     PostQuitMessage(0);
  615.                     return TRUE;
  616.           }
  617.           break;
  618.  
  619.  
  620.        case WM_PAINT:
  621.           // Since we don't register a class, we draw our own
  622.           // icon when we are minimized.
  623.           if(IsIconic(hWnd))
  624.               {
  625.             hDc = BeginPaint(hWnd,&ps);
  626.             DrawIcon(hDc,0,0,hOurIcon);
  627.             EndPaint(hWnd,&ps);
  628.               }
  629.             break;
  630.        case WM_ERASEBKGND:
  631.           // The icon contains some transparent pixels. If we
  632.           // just ignore the erase background message, then
  633.           // DefDlgProc paints the background white. If we
  634.           // swallow the message, then garbage is left in the
  635.           // transparent areas when another window hides the
  636.           // icon and is then moved. Microsoft solved this problem
  637.           // years ago. The solution is in DefWindowProc, case
  638.           // WM_ICONERASEBKGND. So let them do the work!
  639.           if(IsIconic(hWnd))
  640.               {
  641.              return (BOOL)DefWindowProc(hWnd,WM_ICONERASEBKGND,wParam,lParam);
  642.             }
  643.           break;
  644.  
  645.        case WM_DESTROY :
  646.           PostQuitMessage(0);
  647.           break;
  648.  
  649.        default :
  650.           return FALSE;
  651.           }
  652.    return FALSE;
  653. }
  654.  
  655. //---------------------------------------------------------------------------
  656. // Name.......:WinMain
  657. // Purpose....:Create application dialog and dispatch messages.
  658. //---------------------------------------------------------------------------
  659. int PASCAL WinMain(HINSTANCE hInstance,         // Application Instance Handle
  660.                    HINSTANCE hPrevInstance,     // Previous Instance Handle
  661.                    LPSTR  lpszCmdLine,          // Pointer to Command Line
  662.                    int    nCmdShow)             // Show Window Option
  663. {
  664.    static char szAppName[] = "DGC";
  665.    HINSTANCE hInst;
  666.  
  667.  
  668.    MSG      msg;
  669.    HWND     hWndMain;
  670.  
  671.    hInst = hInstance;
  672.    hOurIcon = LoadIcon(hInst,szAppName);
  673.  
  674.    lpfnMainWndProc    = (DLGPROC)MakeProcInstance((FARPROC)MainWndProc,hInst);
  675.  
  676.    lpfnConnectUdpEvent = (CONNECTUDPEVENT)MakeProcInstance((FARPROC)ConnectUdpEvent,
  677.                                                           hInst);
  678.    lpfnExceptionUdpEvent = (EXCEPTIONUDPEVENT)MakeProcInstance((FARPROC)ExceptionUdpEvent,hInst);
  679.    lpfnRecvUdpEvent     = (RECVUDPEVENT)MakeProcInstance((FARPROC)RecvUdpEvent,hInst);
  680.    lpfnSendEvent     = (SENDEVENT)MakeProcInstance((FARPROC)SendEvent,hInst);
  681.  
  682.  
  683.    if(!(hWndMain = CreateDialog(hInst,
  684.                    szAppName,
  685.                    NULL,
  686.                    lpfnMainWndProc)))
  687.    {
  688.         MessageBox(NULL, "Unable to display main dialog", "System Error", MB_OK);
  689.         return FALSE;
  690.    }
  691.  
  692.  
  693.    ShowWindow(hWndMain, nCmdShow);
  694.    UpdateWindow(hWndMain);
  695.  
  696.  
  697.    while(GetMessage(&msg, NULL, 0, 0))
  698.          if(!IsDialogMessage(hWndMain, &msg))
  699.           {
  700.              TranslateMessage(&msg);
  701.              DispatchMessage(&msg);
  702.           }
  703.  
  704.    FreeProcInstance((FARPROC)lpfnMainWndProc);
  705.    FreeProcInstance((FARPROC)lpfnConnectUdpEvent);
  706.    FreeProcInstance((FARPROC)lpfnExceptionUdpEvent);
  707.    FreeProcInstance((FARPROC)lpfnRecvUdpEvent);
  708.    FreeProcInstance((FARPROC)lpfnSendEvent);
  709.  
  710.    return msg.wParam;
  711. }
  712.